const QQMapWX = require('../common/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key: '6BLBZ-W3QWI-VJMG5-5DS45-567A5-CAB2R'
});

export const addr = {
  distance: function (fromLoc, toLocs, cb) {
    const makeError = function (success, msg, result) {
      return {
        success,
        errMsg: msg,
        result
      }
    }
    let param = {
      mode: "driving",
      from: fromLoc, // {longitude, latitude}
      to: toLocs, // []
      success: function (res) { //成功后的回调
        // console.log(res);
        if (res.status != 0) {
          if (cb) cb(makeError(false, "计算距离错误，代码" + res.status))
          return;
        }

        const dis = res.result.elements.map(ele => ele.distance)
        return makeError(true, 'success', dis)
      },
      fail: function (error) {
        console.error(error)
        if (cb) cb(makeError(false, error))
      },
      complete: function (res) {}
    };

    qqmap.calculateDistance(param);
  }
}
export const qqmap = qqmapsdk